function formatTime(date) {
  date = new Date(date * 1000)

  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()

  var hour = date.getHours()
  var minute = date.getMinutes()
  var second = date.getSeconds()


  return [year, month, day, hour, hour].map(formatNumber)
}
function formatNumber(n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}
//日期转换
function formatOrder(order) {
  order.total_parse = formatMoney(order.total)
  order.total_qian = outputmoney(order.total_parse)
  order.measure_amount_par = formatMoney(order.measure_amount)
  order.ship_price_par = formatMoney(order.ship_price)
  order.booking_date_parse = formatTime(order.booking_date)
  order.install_date_parse = formatTime(order.install_date)
  order.state_parse = formatState(order.workflow_state)
  order.states = formatShowState(order.workflow_state)
  order.mount_order_text = formatMountOrder(order.mount_order)
  order.raty_parse = formatRaty(order.raty)
  order.created_at = formatTime(order.created_at)
  order.prices = outputmoney(order.price)
  return order
}
function formatOrderNo(order) {
  order.customer_service_info.raty_parse = formatMoney(order.customer_service_info.raty)
  order.service_info.raty_parse = formatMoney(order.service_info.raty)
}
function formatMoney(money, bool = false) {
  if (!money) {
    money = 0;
  }
  if (bool) {
    money /= 100;
  }
  money = parseFloat(money).toFixed(2)
  return money;
}

function formatMountOrder(bool) {
  if (bool) {
    return '待安装'
  } else {
    return '待测量'
  }
}
function formatData(orders) {
  var temps = []
  orders.forEach(function (order) {
    temps.push(formatOrder(order))
  })
  return temps
}
//订单详情日志
function orderEvents(events) {
  var order_evevts = []
  events.forEach(function (e) {
    e.created_at = formatTime(e.created_at)
    if (e.event == 'appointed_measurement') {
      e.title = "抢单时间"
    }
    if (e.event == 'confirm_installed') {
      e.title = "确认时间"
    }
    if (e.event == 'installed') {
      e.title = "安装时间"
    }
    if (e.event == 'assign') {
      e.title = "指派时间"
    }
    if (e.event == 'appointed_installation') {
      e.title = "审核时间"
    }
    if (e.event == 'measured') {
      e.title = "测量时间"
    }
    order_evevts.push(e)
  })
  return order_evevts
}


function formatState(state) {
  switch (state) {
    case 'new':
      return { state: '新订单', desc: '等待抢单' }
    case 'appointed_measurement':
      return { state: '待测量', desc: '等待上门测量' }
    case 'measured':
      return { state: '待审核', desc: '等待审核' }
    case 'appointed_installation':
      return { state: '待安装', desc: '等待上门安装并上传效果图' }
    case 'installed':
      return { state: '待确认', desc: '等待付款' }
    case 'confirm_installed':
      return { state: '待确认', desc: '等待付款' }
    case 'completed':
      return { state: '已完成', desc: '订单已完成' }
    case 'canceled':
      return { state: '已取消', desc: '订单已取消' }
    default:
      return { state: '无状态', desc: '' }
  }
}
function formatShowState(state) {
  let obj = {
    isCancel: false,
    show_state: false
  }
  switch (state) {
    case 'new':
    case 'appointed_measurement':
    case 'measured':
      obj.isCancel = true
    case 'appointed_installation':
    case 'installed':
      obj.show_state = true
    case 'completed':
    case 'canceled':
  }
  return obj
}
function formatRaty(raty) {
  if (raty == 0) {
    return '100%';
  } else {
    if (raty > 5) { raty = 5 }
    return parseInt(raty / 5 * 100) + '%';
  }
}
function formatComment(comments) {
  let temps = [];
  comments.forEach(function (comment) {
    temps.push(updateCommentTime(comment))
  })
  return temps
}
function updateCommentTime(comment) {
  comment.updated_at_parse = formatTime(comment.updated_at)
  return comment
}

function formatParams(...args) {
  let data = args.shift()
  let params = {}
  for (let v in data) {
    args.map(function (a, b) {
      if (v == a) {
        params[v] = data[v]
      }
    })
  }
  return params;
}
// function toThousands(num) {
//   var result = [], counter = 0;
//   num = (num || 0).toString().split('');
//   for (var i = num.length - 1; i >= 0; i--) {
//     counter++;
//     result.unshift(num[i]);
//     if (!(counter % 3) && i != 0) { result.unshift(','); }
//   }
//   return result.join('');
// }
function outputmoney(number) {
  // number = number.replace(/\,/g, "");
  // if (isNaN(number) || number == "") return "";
  number = Math.round(number * 100) / 100;
  if (number < 0)
    return '-' + outputdollars(Math.floor(Math.abs(number) - 0) + '') + outputcents(Math.abs(number) - 0);
  else
    return outputdollars(Math.floor(number - 0) + '') + outputcents(number - 0);
}
function outputdollars(number) {
  if (number.length <= 3)
    return (number == '' ? '0' : number);
  else {
    var mod = number.length % 3;
    var output = (mod == 0 ? '' : (number.substring(0, mod)));
    for (var i = 0; i < Math.floor(number.length / 3); i++) {
      if ((mod == 0) && (i == 0))
        output += number.substring(mod + 3 * i, mod + 3 * i + 3);
      else
        output += ',' + number.substring(mod + 3 * i, mod + 3 * i + 3);
    }
    return (output);
  }
}
function outputcents(amount) {
  amount = Math.round(((amount) - Math.floor(amount)) * 100);
  return (amount < 10 ? '.0' + amount : '.' + amount);
}

module.exports = {
  formatOrder: formatOrder,
  formatTime: formatTime,
  formatData: formatData,
  formatMoney: formatMoney,
  formatRaty: formatRaty,
  formatComment: formatComment,
  formatParams: formatParams,
  formatOrderNo: formatOrderNo,
  orderEvents: orderEvents,
  outputmoney: outputmoney
}

